from rest_framework import serializers
from .models import Address
from .utils import validate_city_pair

class AddressSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(
        default=serializers.CurrentUserDefault()
    )
    
    class Meta:
        model = Address
        fields = '__all__'
        extra_kwargs = {
            'user': {'read_only': True}
        }
    
    def validate(self, data):
        # 验证省-市匹配关系
        province = data.get('province')
        city = data.get('city')
        
        if province and city and not validate_city_pair(province, city):
            raise serializers.ValidationError(
                f"无效的城市组合: {province}-{city}"
            )
        
        return data